package com.dragon.app.ykd.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.app.commons.constant.ErrorCode;
import com.dragon.app.commons.constant.ThirdResponseConstant;
import com.dragon.app.ykd.constant.ConstantYKD;
import com.dragon.loan.entity.AcLoanApplyInfo;
import com.dragon.loan.service.LoanInfoIDragonService;
import com.fintech.common.util.ApiResponse;
import com.fintech.common.vo.FintechResult;
import com.fintech.third.model.bairong.CurrentEquipmentPackageInfo;
import com.fintech.third.model.bairong.LoanFacilityPackageInfo;
import com.fintech.third.model.bairong.LoginFacilityPackageInfo;
import com.fintech.third.model.bairong.RegisterFacilityPackageInfo;
import com.fintech.third.service.bairong.BaiRongService;
import com.fintech.third.service.bairong.OneCreditCardService;
import com.fintech.third.utils.SerialNumCreater;
import com.marvels.common.utils.MarvelsLogUtil;

import net.sf.json.JSONObject;

/**
 * 设备反欺诈
 * 
 * @author tangpw
 *
 */
@Service("equipmentAgainstFraudAppService")
public class EquipmentAgainstFraudServiceImpl implements EquipmentAgainstFraudService{
	@Autowired
	private LoanInfoIDragonService loanInfoIDragonService;
	@Autowired
	private BaiRongService BaiRong;
	@Autowired
	private OneCreditCardService OneCreditCard;

	/**
	 * 提现设备反欺诈
	 * 
	 * @param map
	 * loanId：进件流水号
	 * mobile：电话号码
	 * afSwiftNumber：前端集成百融sdk生成的唯一标示
	 * interfaceName：接口类型名称ykd，bdd
	 * @return
	 */
	@Override
	public FintechResult cashFacilityInformation( Map<String, Object> map) {
		FintechResult fintechResult = new FintechResult();
		MarvelsLogUtil.getInstance().info("==========提现设备反欺诈查询请求参数:" + JSONObject.fromObject(map));
		String loanId = map.get("loanId").toString();
		String mobile = map.get("mobile").toString();
		String interfaceName = null;
		if(null!=map.get("interfaceName")){
			interfaceName=map.get("interfaceName").toString();
		}
		String afSwiftNumber = map.get("afSwiftNumber").toString();
		//生成请求流水号
		String serialNum = SerialNumCreater.creater("Br", loanId);
		//获取单子详情
		AcLoanApplyInfo acLoanApplyInfo = loanInfoIDragonService.queryLoanBasicInfo(loanId);
		Map<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("serialNum", serialNum);
		hashMap.put("loanId", loanId);
		hashMap.put("channel", "app");
		hashMap.put("mobile", mobile);
		hashMap.put("custIdCard", acLoanApplyInfo.getCustIdCard());
		hashMap.put("name", acLoanApplyInfo.getCustName());
		hashMap.put("afSwiftNumber", afSwiftNumber);
		ApiResponse<CurrentEquipmentPackageInfo> apiResponse = new ApiResponse<CurrentEquipmentPackageInfo>();
		// 是一卡贷
		if (ConstantYKD.INTERFACENAME_YKD.equals(interfaceName)){
			apiResponse=OneCreditCard.cashFacilityInformationReportPack(hashMap);
		} else if(ConstantYKD.INTERFACENAME_BDD.equals(interfaceName)|| StringUtils.isEmpty(interfaceName)){//保单贷
			AcLoanApplyInfo info = loanInfoIDragonService.queryLoanBasicInfo(loanId);
			String productType = info.getProductId();
			hashMap.put("productType", productType);
			apiResponse= BaiRong.cashFacilityInformationReportPack(hashMap);
		}
		MarvelsLogUtil.getInstance().info("==========提现设备反欺诈查询返回参数:" + JSONObject.fromObject(apiResponse));
		if (ThirdResponseConstant.SUCCESS.getCode() == apiResponse.getCode()) {
			fintechResult.setErrorCode(ErrorCode.SUCCESS);
			fintechResult.setMessage(apiResponse.getMsg());
		} else {
			fintechResult.setErrorCode(ErrorCode.FAIL);
			fintechResult.setMessage(apiResponse.getMsg());
		}
		return fintechResult;
	}

	/**
	 * 借款设备反欺诈
	 * 
	 * @param map
	 * loanId：进件流水号
	 * mobile：电话号码
	 * afSwiftNumber：前端集成百融sdk生成的唯一标示
	 * interfaceName：接口类型名称ykd，bdd
	 * @return
	 */
	@Override
	public FintechResult loanFacilityInformationReport( Map<String, Object> map) {
		FintechResult fintechResult = new FintechResult();
		MarvelsLogUtil.getInstance().info("==========借款设备反欺诈查询请求参数:" + JSONObject.fromObject(map));
		String mobile = map.get("mobile").toString();
		String loanId = map.get("loanId").toString();
		String interfaceName = null;
		if(null!=map.get("interfaceName")){
			interfaceName=map.get("interfaceName").toString();
		}
		String afSwiftNumber = map.get("afSwiftNumber").toString();
		//获取单子详情
		AcLoanApplyInfo acLoanApplyInfo = loanInfoIDragonService.queryLoanBasicInfo(loanId);
		//生成请求流水号
		String serialNum = SerialNumCreater.creater("Br", loanId);
		Map<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("serialNum", serialNum);
		hashMap.put("loanId", loanId);
		hashMap.put("channel", "app");
		hashMap.put("mobile", mobile);
		hashMap.put("name", acLoanApplyInfo.getCustName());
		hashMap.put("custIdCard", acLoanApplyInfo.getCustIdCard());
		hashMap.put("afSwiftNumber", afSwiftNumber);
		ApiResponse<LoanFacilityPackageInfo> apiResponse = new ApiResponse<LoanFacilityPackageInfo>();
		// 是一卡贷
		if (ConstantYKD.INTERFACENAME_YKD.equals(interfaceName)){
			apiResponse=OneCreditCard.loanFacilityInformationReportPack(hashMap);
		} else if(ConstantYKD.INTERFACENAME_BDD.equals(interfaceName)|| StringUtils.isEmpty(interfaceName)){
			AcLoanApplyInfo info = loanInfoIDragonService.queryLoanBasicInfo(loanId);				
			String productType = info.getProductId();
			hashMap.put("productType", productType);
		   apiResponse= BaiRong.loanFacilityInformationReportPack(hashMap);
		}
		MarvelsLogUtil.getInstance().info("==========借款设备反欺诈查询返回参数:" + JSONObject.fromObject(apiResponse));
		if (ThirdResponseConstant.SUCCESS.getCode() == apiResponse.getCode()) {
			fintechResult.setErrorCode(ErrorCode.SUCCESS);
			fintechResult.setMessage(apiResponse.getMsg());
		} else {
			fintechResult.setErrorCode(ErrorCode.FAIL);
			fintechResult.setMessage(apiResponse.getMsg());
		}
		return fintechResult;
	}

	/**
	 * 登录设备反欺诈
	 * 
	 * @param map
	 * mobile：电话号码
	 * afSwiftNumber：前端集成百融sdk生成的唯一标示
	 * interfaceName：接口类型名称ykd，bdd
	 * @return
	 */
	@Override
	public FintechResult loginFacilityInformationReport( Map<String, Object> map) {
		FintechResult fintechResult = new FintechResult();
		MarvelsLogUtil.getInstance().info("==========登录设备反欺诈查询请求参数:" + JSONObject.fromObject(map));
		String mobile = map.get("mobile").toString();
		String afSwiftNumber = map.get("afSwiftNumber") == null ? null : map.get("afSwiftNumber").toString();
		String interfaceName = null;
		if(null!=map.get("interfaceName")){
			interfaceName=map.get("interfaceName").toString();
		}
		Map<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("channel", "app");
		hashMap.put("mobile", mobile);
		hashMap.put("afSwiftNumber", afSwiftNumber);
		ApiResponse<LoginFacilityPackageInfo> apiResponse=new ApiResponse<LoginFacilityPackageInfo>();
		// 是一卡贷
		if (ConstantYKD.INTERFACENAME_YKD.equals(interfaceName)){
			apiResponse=OneCreditCard.loginFacilityInformationReportPack(hashMap);
		} else if(ConstantYKD.INTERFACENAME_BDD.equals(interfaceName)|| StringUtils.isEmpty(interfaceName)){
			AcLoanApplyInfo info = loanInfoIDragonService.queryLoanBasicInfo((String) map.get("loanId"));				
			String productType = info.getProductId();
			hashMap.put("productType", productType);
			apiResponse= BaiRong.loginFacilityInformationReportPack(hashMap);
		}
	
		MarvelsLogUtil.getInstance().info("==========登录设备反欺诈查询返回参数:" + JSONObject.fromObject(apiResponse));
		if (ThirdResponseConstant.SUCCESS.getCode() == apiResponse.getCode()) {
			fintechResult.setErrorCode(ErrorCode.SUCCESS);
			fintechResult.setMessage(apiResponse.getMsg());
		} else {
			fintechResult.setErrorCode(ErrorCode.FAIL);
			fintechResult.setMessage(apiResponse.getMsg());
		}
		return fintechResult;
	}

	/**
	 * 注册设备反欺诈
	 * 
	 * @param map
	 * mobile：电话号码
	 * afSwiftNumber：前端集成百融sdk生成的唯一标示
	 * interfaceName：接口类型名称ykd，bdd
	 * @return
	 */
	@Override
	public FintechResult registerFacilityInformationReport( Map<String, Object> map) {
		FintechResult fintechResult = new FintechResult();
		MarvelsLogUtil.getInstance().info("==========注册设备反欺诈查询请求参数:" + JSONObject.fromObject(map));
		String mobile = map.get("mobile").toString();
		String afSwiftNumber = map.get("afSwiftNumber").toString();
		String interfaceName = null;
		if(null!=map.get("interfaceName")){
			interfaceName=map.get("interfaceName").toString();
		}
		Map<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("channel", "app");
		hashMap.put("mobile", mobile);
		hashMap.put("afSwiftNumber", afSwiftNumber);
		
		ApiResponse<RegisterFacilityPackageInfo> apiResponse=new ApiResponse<RegisterFacilityPackageInfo>();
		// 是一卡贷
		if (ConstantYKD.INTERFACENAME_YKD.equals(interfaceName)){
			apiResponse=OneCreditCard.registerFacilityInformationReportPack(hashMap);
		} else if(ConstantYKD.INTERFACENAME_BDD.equals(interfaceName)|| StringUtils.isEmpty(interfaceName)){
			AcLoanApplyInfo info = loanInfoIDragonService.queryLoanBasicInfo((String) map.get("loanId"));				
			String productType = info.getProductId();
			hashMap.put("productType", productType);
			apiResponse=BaiRong.registerFacilityInformationReportPack(hashMap);
		}
		if (ThirdResponseConstant.SUCCESS.getCode() == apiResponse.getCode()) {
			fintechResult.setErrorCode(ErrorCode.SUCCESS);
			fintechResult.setMessage(apiResponse.getMsg());
		} else {
			fintechResult.setErrorCode(ErrorCode.FAIL);
			fintechResult.setMessage(apiResponse.getMsg());
		}
		return fintechResult;
	}

}
